package com.seal.yoyoshop.service.impl;

import com.seal.yoyoshop.bin.Goods;
import com.seal.yoyoshop.bin.Item;
import com.seal.yoyoshop.bin.Order;
import com.seal.yoyoshop.bin.User;
import com.seal.yoyoshop.dao.ItemDao;
import com.seal.yoyoshop.dao.OrderDao;
import com.seal.yoyoshop.dao.impl.ItemDaoImpl;
import com.seal.yoyoshop.dao.impl.OrderDaoImpl;
import com.seal.yoyoshop.service.ItemService;
import com.seal.yoyoshop.service.OrderService;
import com.seal.yoyoshop.service.UserService;

import java.util.Date;
import java.util.List;

public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao = new OrderDaoImpl();
    private ItemService itemService = new ItemServiceImpl();
    private UserService userService = new UserServiceImpl();
    @Override
    public void addGoodsToOrder(Goods good, Order order) {
//        获取商品id
        int goodId = good.getId();
//        获取购买项列表并遍历
        List<Item> itemList = order.getItemList();
        boolean is_add = false;
        for(int i = 0;i<itemList.size();i++){
            Item item = itemList.get(i);
            if(item.getGoodId() == goodId){//商品曾加过
//                修改购物车商品数量及总金额
                item.setAmount(item.getAmount() + 1);
                order.setAmount(order.getAmount() + 1);
                order.setTotal(order.getTotal() + good.getPrice());

                is_add = true;
                break;
            }
        }
        if(!is_add){
            Item item = new Item(good.getPrice(), 1, goodId, good);
            itemList.add(item);
            // 修改购物车商品数量及总金额
            order.setAmount(order.getAmount() + 1);
            order.setTotal(order.getTotal() + good.getPrice());
        }

    }

    @Override
    public boolean lessonGoodsToOrder(Goods goods, Order order) {
//        获取商品id
        int goodId = goods.getId();
//        获取购买项列表并遍历
        List<Item> itemList = order.getItemList();
        for(int i = 0;i<itemList.size();i++){
            Item item = itemList.get(i);
            if(item.getGoodId() == goodId){//商品曾加过
//                修改购物车商品数量及总金额
                item.setAmount(item.getAmount() - 1);
                order.setAmount(order.getAmount() - 1);
                order.setTotal(order.getTotal() - goods.getPrice());
                if(item.getAmount() == 0){
                    itemList.remove(i);
                }
                break;
            }
        }
        if(itemList.size() == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean DeleteGoodsToOrder(Goods goods, Order order) {
//        获取商品id
        int goodId = goods.getId();
//        获取购买项列表并遍历
        List<Item> itemList = order.getItemList();
        for(int i = 0;i<itemList.size();i++){
            Item item = itemList.get(i);
            if(item.getGoodId() == goodId){//商品曾加过
//                修改购物车商品数量及总金额
                order.setAmount(order.getAmount() - item.getAmount());
                order.setTotal(order.getTotal() - goods.getPrice()*item.getAmount());

                itemList.remove(i);
                break;
            }
        }
        if(itemList.size() == 0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean addOrder(Order order) {
        order.setStatus(1);
        order.setPaytype(0);
        order.setSystime(new Date());
        boolean isSuccess = orderDao.InsertOrder(order);
        return isSuccess;
    }

    @Override
    public int getIdByOrder(Order order) {
        int id = orderDao.SelectidByOrder(order);
        return id;
    }

    @Override
    public Order getOrderById(int orderId) {
        Order order = orderDao.SelectOrderById(orderId);
        return order;
    }

    @Override
    public boolean updateOrder(Order order) {
        boolean isSuccess = orderDao.updateOrder(order);

        return isSuccess;
    }

    @Override
    public List<Order> getOrderByUserId(Integer userid) {
        List<Order> orderList = orderDao.SelectOrderByUserId(userid);
        for(int i = 0 ;i < orderList.size();i++){//查订单内部商品
            Order order = orderList.get(i);
            int orderId = order.getId();
            List<Item> itemList = itemService.getItemListByOrderId(orderId);
            order.setItemList(itemList);
        }
        return orderList;
    }

    @Override
    public List<Order> getOrderList(Integer page, Integer size) {
        List<Order> orderList = orderDao.SelectOrderList(page,size);
        for(int i = 0 ; i < orderList.size(); i ++){
            //查订单内部商品
            Order order = orderList.get(i);
            int orderId = order.getId();
            List<Item> itemList = itemService.getItemListByOrderId(orderId);
            order.setItemList(itemList);
            //获取用户id
            int userId = order.getUserId();
            User user = userService.getUserById(userId);
            order.setUser(user);
        }
        return orderList;
    }

    @Override
    public long getOrderCount() {
        long count =orderDao.selectCount();
        return count;
    }

    @Override
    public List<Order> getOrderList(Integer status, Integer page, Integer size) {
        List<Order> orderList = orderDao.SelectOrderList(status, page, size);
        for(int i = 0 ; i < orderList.size(); i ++){
            //查订单内部商品
            Order order = orderList.get(i);
            int orderId = order.getId();
            List<Item> itemList = itemService.getItemListByOrderId(orderId);
            order.setItemList(itemList);
            //获取用户id
            int userId = order.getUserId();
            User user = userService.getUserById(userId);
            order.setUser(user);
        }
        return orderList;
    }

    @Override
    public long getOrderCount(Integer status) {
        long count =orderDao.selectCount(status);
        return count;
    }
}
